/**
 * Created with JetBrains PhpStorm.
 * User: BGate
 * Date: 26/10/2012
 * Time: 12:25
 * To change this template use File | Settings | File Templates.
 */

Crafty.scene("Load", function () {
    //Crafty.background('url("assets/bgr.png")');
    var assets = toLoad, loaded = 0,
        loadText = Crafty.e("2D,DOM,Text,loadingtext").text("0%").attr({x: 480, y: 250})
            .bind("EnterFrame", function (f) {
                if (this.loaded < loaded && f.frame % 2 === 0) {
                    this.loaded++;
                    this.text(this.loaded + "%");
                }
            }),
        progress = function (data) {
            loaded = Math.floor(data.percent);
            console.log("loaded " + data.loaded);
        },
        error = function (data) {
            console.log("error " + data.loaded);
        };
    loadText.loaded = 0;
    Crafty.load(assets, function () {
        //	Crafty.scene("Menu");
        //makeSprites();
        Crafty.scene("menu");
    }, progress, error);

    var loadingText = Crafty.e("2D,Text,DOM,loadingtext").text("Loading...")
        .attr({x: 200, y: 250})
        .bind('EnterFrame', function (frame) {
            var f = frame.frame % 40;
            if (f % 10 == 0) {
                this.text("Loading." + "...".substr(0, f / 10));
            }
        });
    Crafty.e("2D,DOM,Text,bgate").text("B-GATE CREATION NONSTOP").attr({x: 90, y: 500, w: 800});
});



Crafty.scene("menu",function(){

    //Crafty.e("Backgroud, DOM, 2D, Image").image("menu.jpg","no-repeat")
    //      .attr({w: flowW, h: flowH});
    //Crafty.e("ChoseMode, DOM, 2D,MyButton").button("Chose Mode",4,0);
    Crafty.e("6x6,2D, DOM, MyButton").button("Play 6x6 Boards",1,6);

    Crafty.e("8x8,2D, DOM, MyButton").button("Play 8x8 Boards",2,8);
    Crafty.e("10x10,2D, DOM, MyButton").button("Play 10x10 Boards",3,10);
    //Crafty.scene("play");
});


Crafty.scene("GameSelect",function(){
    Crafty.e("DOM,2D,TouchListener")
        .touchListener(
            0,
            0,
            flowW,
            flowH,
            function(){
                if(gamePageSelect>0){
                    gamePageSelect--;
                    Crafty.scene("GameSelect");
                }else{
                    Crafty.scene("menu");
                }
            },
            function(){

                if(gamePageSelect<temp-1){
                    gamePageSelect++;
                    Crafty.scene("GameSelect");
                }else{
                    mapSelect = 0;
                    pointMap = pointMaps.getMap(mapSelect);
                    Crafty.scene("Play");
                }
            },
            30);


    Crafty.e("DOM,2D,Text")
        .text("Select Game")
        .attr({x: 200, y: 20});
    var pagesize = 8;
    if(compressMaps[cellN].length>pagesize){
        var temp = Math.floor(compressMaps[cellN].length/pagesize)+1;
        if(gamePageSelect>=temp) gamePageSelect = temp - 1;

        var tempn = Math.min(compressMaps[cellN].length,((gamePageSelect+1)*pagesize -1));
        for(var i = gamePageSelect*pagesize; i< tempn ; i++){
            Crafty.e("DOM, 2D, gameTiles").tiles(i-gamePageSelect*pagesize,i);
        }

         /*   Crafty.e("DOM,2D,Image,Mouse")
                .attr({x: 0, y: 100,h: 470})
                .image("images/previous1.png")
                .bind("MouseDown",function(){
                    if(gamePageSelect>0){
                        gamePageSelect--;
                        Crafty.scene("GameSelect");
                    }else{
                        Crafty.scene("menu");
                    }

                });

        if(gamePageSelect != temp-1){
            Crafty.e("DOM,2D,Image,Mouse")
                .attr({x: 420, y: 100, h: 470})
                .image("images/next1.png")
                .bind("MouseDown",function(){
                    if(gamePageSelect<temp-1)
                        gamePageSelect++;
                        Crafty.scene("GameSelect");

                });
        }*/



    }else{
        for(var i = 0 ; i < compressMaps[cellN].length; i++ ){
            Crafty.e("DOM, 2D, gameTiles").tiles(i,i);
        }
    }

    Crafty.e("DOM,2D,Image,Mouse")
        .attr({x: 0, y: 0})
        .image("images/back.png")
        .bind("MouseDown",function(){
            Crafty.scene("menu");
        });

});

Crafty.scene("Play",function(){



    //Crafty.background('rgb(127,127,127)');
    var tempx = Math.floor(flowW/cellN);
    var tempy = Math.floor(flowH/cellN)
    cellSize = Math.min(tempx, tempy);
    if(tempx<tempy) isWitdhMode = false;
    else isWitdhMode = true;
    if(isWitdhMode){
        baseX = Math.round( (flowW-cellN*cellSize)/2 );
        baseY = Math.round( (flowH-cellN*cellSize) / 2 );
    }else{
        baseX = Math.round( (flowW-cellN*cellSize)/2 );
        baseY = Math.round( (flowH-cellN*cellSize) / 2 );
    }
    for(i=0;i<cellN;i++){
        for(j=0;j<cellN;j++){
            posi = new position(i,j);
            map[posi.tran]=Crafty.e("DOM,2D,Cell,Cell"+i+" "+j)
                .attr({x: baseX + i*cellSize, y: baseY + j*cellSize,w: cellSize-1,h: cellSize-1})
                .cell(posi);

        }
    }
    TimeCount=0;

    Crafty.e("DOM,2D,TouchListener")
        .touchListener(0,560,480,80,
            function(){
                if(mapSelect != 0){
                    mapSelect--;
                }else{
                    if(cellN!=6){
                        cellN-=2;
                        mapSelect = pointMaps.length() - 1;
                        //pointMaps = pointMapSets[cellN];
                    }

                    pointMap = pointMaps.getMap(mapSelect);
                }
                resetPath();
                pointMap = pointMaps.getMap(mapSelect);

                Crafty.scene("Play");
            },
            function(){
                if(mapSelect <pointMaps.length()-1 ){
                    mapSelect++;
                }else{

                    if(cellN!=10){
                        cellN+=2;
                        mapSelect = 0;
                        //pointMaps = pointMapSets[cellN];
                    }
                }
                pointMap = pointMaps.getMap(mapSelect);
                resetPath();
                Crafty.scene("Play");
            },
            90
        );


    Crafty.e("DOM,2D,InGameBut").inGameBut("text",BACK);
    Crafty.e("DOM,2D,InGameBut").inGameBut("text",RESET);
    Crafty.e("DOM,2D,InGameBut").inGameBut("text",NEXT);
    Crafty.e("DOM,2D,InGameBut").inGameBut("text",PRE);
    Crafty.e("DOM, 2D, Text, Level")
        .text("Level: "+(mapSelect+1)+"/"+pointMaps.length())
        .attr({x:0 , y: Math.floor(baseY/3), w: flowW/4, h: baseY});
    Crafty.e("DOM, 2D, Text, BoardName")
        .text("Board "+cellN+"x "+cellN)
        .attr({x: Math.round(2*flowW/5), y: Math.floor(baseY/3),w: flowW/4, h: baseY});
    Crafty.e("DOM, 2D, Text, Times")
        .text("Times: 0s/-")
        .attr({x: Math.round(3*flowW/4) , y: Math.floor(baseY/3), w: flowW/4, h: baseY});
    clearInterval(timing);
    timing = setInterval(function(){
        TimeCount++;
        Crafty("Times").each(function(){
            if(played[cellN][mapSelect] == 0){
                this.text("Times: "+TimeCount+"s/-");
            }else{
                this.text("Times: "+TimeCount+"s/"+played[cellN][mapSelect]+"s");
            }

        });

    },1000);
});

console.log("Loaded scene");